package models

import (
	"fmt"
)

//作业02_给定一个字符串，统计其中所有类型字符出现的次数
func getHW2(s string) {
	var arr = GetFrequency(s)
	var marr = GetContainsNum(s)
	fmt.Println("该字符串出现控制字符的次数为", arr[0])
	fmt.Println("该字符串出现数字字符的次数为", arr[1])
	fmt.Println("该字符串出现字母字符的次数为", arr[2])
	fmt.Println("该字符串出现其他字符的次数为", arr[3])
	for key, val := range marr {
		fmt.Println(string(key), ":", val)
	}
}

func GetFrequency(s string) []int {
	var arr = make([]int, 4)
	sarr := []byte(s)
	for i := 0; i < len(sarr); i++ {
		switch {
		case 0 <= sarr[i] && sarr[i] <= 32:
			arr[0]++
		case 48 <= sarr[i] && sarr[i] <= 57:
			arr[1]++
		case 65 <= sarr[i] && sarr[i] <= 90:
			arr[2]++
		case 97 <= sarr[i] && sarr[i] <= 122:
			arr[2]++
		case sarr[i] == 127:
			arr[0]++
		default:
			fmt.Println(sarr[i])
			arr[3]++
		}
	}

	return arr
}

//获取每个的出现次数(map)
func GetContainsNum(s string) map[rune]int {
	var arr = []rune(s)
	var maps = make(map[rune]int)
	for i := 0; i < len(arr); i++ {
		maps[arr[i]]++
	}
	return maps
}
